package com.gy.mbaselibrary.views.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gy.mbaselibrary.R;
import com.gy.mbaselibrary.utils.ScreenUtil;
import com.gy.mbaselibrary.utils.StringUtil;

public class ConfirmDialog extends Dialog {
    private static Builder builder;
    private TextView tv_right,tv_content,tv_title;
    private ConfirmListener confirmListener;
    private Object tag;

    public void setConfirmListener(ConfirmListener confirmListener) {
        this.confirmListener = confirmListener;
    }


    public void setTag(Object tag) {
        this.tag = tag;
    }

    public static class Builder{
        private String titleStr;
        private String content;
        private String rightStr;
        private int leftColor;
        private int rithgColor;
        private Context context;
        private int height = -1;
        private int width = -1;
        private int bgResource = -1;

        private Builder(){

        }

        public int getBgResource() {
            return bgResource;
        }

        public Builder setBgResource(int bgResource) {
            this.bgResource = bgResource;
            return this;
        }


        private String getRightStr() {
            return rightStr;
        }

        public Builder setRightStr(String rightStr) {
            this.rightStr = rightStr;
            return this;
        }

        private String getTitleStr() {
            return titleStr;
        }

        public Builder setTitleStr(String titleStr) {
            this.titleStr = titleStr;
            return this;
        }

        private String getContent() {
            return content;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        private int getLeftColor() {
            return leftColor;
        }

        public Builder setLeftColor(int leftColor) {
            this.leftColor = leftColor;
            return this;
        }

        private int getRithgColor() {
            return rithgColor;
        }

        public Builder setRithgColor(int rithgColor) {
            this.rithgColor = rithgColor;
            return this;
        }

        private Context getContext() {
            return context;
        }

        private Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        private int getHeight() {
            return height;
        }

        public Builder setHeight(int height) {
            this.height = height;
            return this;
        }

        private int getWidth() {
            return width;
        }

        public Builder setWidth(int width) {
            this.width = width;
            return this;
        }

        public ConfirmDialog create(){
            ConfirmDialog commonDialog = new ConfirmDialog(context);
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_confirm_base,null);
            TextView tv_left = view.findViewById(R.id.tv_left);
            TextView tv_right = view.findViewById(R.id.tv_right);
            TextView tv_content = view.findViewById(R.id.tv_content);
            TextView tv_title = view.findViewById(R.id.tv_title);
            LinearLayout ll_dialog = view.findViewById(R.id.ll_dialog);
            commonDialog.setContentView(view);


            if (StringUtil.isEmpty(titleStr)){
                tv_title.setVisibility(View.GONE);
            }else {
                tv_title.setText(titleStr);
            }
            if (StringUtil.isEmpty(content)){
                tv_content.setVisibility(View.VISIBLE);
            }else {
                tv_content.setText(content);
            }
            if (leftColor != 0){
                tv_left.setTextColor(leftColor);
            }

            if (rithgColor != 0){
                tv_right.setTextColor(rithgColor);
            }
            if (!StringUtil.isEmpty(rightStr)){
                tv_right.setText(rightStr);
            }

            if (bgResource != - 1){
                ll_dialog.setBackgroundResource(bgResource);
            }

            commonDialog.setCancelable(true);//点击外部不可dismiss
            commonDialog.setCanceledOnTouchOutside(true);

            Window window = commonDialog.getWindow();
            window.setGravity(Gravity.CENTER);
            WindowManager.LayoutParams params = window.getAttributes();
            if (width == -1){
                params.width = ScreenUtil.getScreenWidth(getContext()) - ScreenUtil.dp2px(16,getContext()) * 2;
            }else {
                params.width = width;
            }
            if (height == -1){
                params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            }else {
                params.height = height;
            }

            window.setAttributes(params);
            window.setBackgroundDrawable(null);
            //设置半透明度
            window.setDimAmount(0.5f);
            commonDialog.setTv_right(tv_right);
            commonDialog.setTv_content(tv_content);

            return commonDialog;
        }
    }



    public TextView getTv_content() {
        return tv_content;
    }

    public void setTv_content(TextView tv_content) {
        this.tv_content = tv_content;
    }

    public TextView getTv_title() {
        return tv_title;
    }

    public void setTv_title(TextView tv_title) {
        this.tv_title = tv_title;
    }


    public TextView getTv_right() {
        return tv_right;
    }

    public void setTv_right(TextView tv_right) {
        this.tv_right = tv_right;
        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (confirmListener != null)
                    confirmListener.confirmListener(tag);
            }
        });
    }

    public static Builder createBuilder(Context context){
        builder = new Builder();
        return builder.setContext(context);
    }

    public ConfirmDialog(Context context) {
        super(context);
    }

    public ConfirmDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected ConfirmDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public interface ConfirmListener{
        void confirmListener(Object tag);
    }

}
